import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import Welcome from '../components/Welcome.vue'
// import Users from '../components/user/Users.vue'
// import Rights from '../components/power/Rights.vue'
// import Roles from '../components/power/Roles.vue'
// import Categories from '../components/goods/Categories.vue'
// import Goods from '../components/goods/Goods.vue'
// import GoodsAdd from '../components/goods/GoodsAdd.vue'
// import Params from '../components/goods/Params.vue'
// import Order from '../components/order/Order.vue'
// import report from '../components/report/Report.vue'
// import User from '../components/yach/User.vue'

// 路由懒加载
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome.vue')

const Users = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/user/Users.vue')
const Rights = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/power/Roles.vue')

const Categories = () => import(/* webpackChunkName: "cate_goods_goodsAdd_params" */ '../components/goods/Categories.vue')
const Goods = () => import(/* webpackChunkName: "cate_goods_goodsAdd_params" */ '../components/goods/Goods.vue')
const GoodsAdd = () => import(/* webpackChunkName: "cate_goods_goodsAdd_params" */ '../components/goods/GoodsAdd.vue')
const Params = () => import(/* webpackChunkName: "cate_goods_goodsAdd_params" */ '../components/goods/Params.vue')

const Order = () => import(/* webpackChunkName: "order_report_user" */ '../components/order/Order.vue')
const report = () => import(/* webpackChunkName: "order_report_user" */ '../components/report/Report.vue')
const User = () => import(/* webpackChunkName: "order_report_user" */ '../components/yach/User.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redicect: '/login' },       // 默认访问 login 页面 
  { path: '/login', component: Login },    // login路径，指定对应的Login.vue
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',                         // 重定向到 welcome 页面
    children: [
      { path: '/welcome', component: Welcome },         // 欢迎 子页面
      { path: '/users', component: Users },             // 用户管理 → 用户列表
      { path: '/roles', component: Roles },             // 权限管理 → 角色列表
      { path: '/rights', component: Rights },           // 权限管理 → 权限列表
      { path: '/goods', component: Goods },             // 商品管理 → 商品列表
      { path: '/goods/add', component: GoodsAdd },      // 商品管理 → 商品列表-添加页面
      { path: '/params', component: Params },           // 商品管理 → 分类参数
      { path: '/categories', component: Categories },   // 商品管理 → 商品分类
      { path: '/orders', component: Order },            // 订单管理 → 订单列表
      { path: '/reports', component: report },          // 数据统计 → 数据报表
      { path: '/yachUser', component: User },           // YACH相关 → 用户信息
    ]
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {      // 挂在路由守卫
  if (to.path === '/login') return next()    // 如果在登录页面，则直接访问

  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')       // 如果页面seeion中不存在token,则跳转至登录页面
  next()
})

export default router
